package com.easygem.controller.front;

import com.easygem.common.annotation.ControllerEndpoint;
import com.easygem.common.error.SystemException;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.vo.front.SourceVo;
import com.easygem.front.service.SourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author luoxi
 * @version 1.0
 * @className SourceController
 * @Description 来源控制层
 * @date 2024/12/26 21:33
 */
@Api(tags = "来源")
@RestController
@RequestMapping("/system/source")
public class SourceController {
    @Autowired
    private SourceService sourceService;
    /**
     * 所有来源
     * @return
     */
    @ApiOperation(value = "所有来源")
    @GetMapping("/findAll")
    @ResponseBody
    public ResponseBean<List<SourceVo>> findAll(@RequestParam(value = "accountName", defaultValue = "") String accountName) {
        List<SourceVo> sourceVoList = sourceService.getSourceAll(accountName);
        return ResponseBean.success(sourceVoList);
    }
    /**
     * 来源下拉列表框
     *
     * @return
     */
    @ApiOperation(value = "来源列表", notes = "通过来源id查询")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<List<SourceVo>> findList(@RequestParam(value = "accountName", defaultValue = "") String accountName) {
        List<SourceVo> sourceVoList = sourceService.getSourceListByEnable(accountName);
        return ResponseBean.success(sourceVoList);
    }
    /**
     * 添加来源
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加来源失败", operation = "添加来源")
    @RequiresPermissions({"DataDictionary:add"})
    @ApiOperation(value = "添加来源")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean add(@RequestBody @Validated SourceVo sourceVo) {
        sourceService.add(sourceVo);
        return ResponseBean.success();
    }


    /**
     * 编辑来源
     *
     * @param id 编号
     * @return
     */
    @ApiOperation(value = "编辑来源")
    @RequiresPermissions({"DataDictionary:edit"})
    @GetMapping("/edit/{id}")
    @ResponseBody
    public ResponseBean edit(@PathVariable Integer id) throws SystemException {
        SourceVo sourceVo = sourceService.edit(id);
        return ResponseBean.success(sourceVo);
    }

    /**
     * 更新来源操作
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新来源失败", operation = "更新来源")
    @ApiOperation(value = "更新来源")
    @RequiresPermissions({"source:update"})
    @PutMapping("/update/{id}")
    @ResponseBody
    public ResponseBean update(@PathVariable Integer id,@RequestBody @Validated SourceVo sourceVo) throws SystemException {
        System.out.println("修改");
        sourceService.update(id,sourceVo);
        return ResponseBean.success();
    }
}
